package com.test.test.utils;


import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * redicache 工具类
 * 
 */
@SuppressWarnings("unchecked")
@Component
public class RedisUtils {
    
    @SuppressWarnings("rawtypes")
    @Autowired
    private RedisTemplate redisTemplate;
    
    
   // StringRedisTemplate

    /**
     * 批量删除对应的value
     * 
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     * 
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }

    /**
     * 删除对应的value
     * 
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     * 
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     * 
     * @param key
     * @return
     */
    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 写入缓存
     * 不建议    应设置缓存有效期
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存
     * 
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * String  
     * 锁
     * @param key
     * @param value
     * @return
     */
    public Boolean lockString(final String key, Object value){
    	ValueOperations opsForValue = redisTemplate.opsForValue();
		Boolean setIfAbsent = opsForValue.setIfAbsent(key, value);
		if(setIfAbsent){
			return true;
		}
		String currentValue = (String) get(key);
		//判断锁是否过期
		if (CoreUtils.isNotEmpty(currentValue) 
				&& Long.parseLong(currentValue) < System.currentTimeMillis()) {
			//获取上一个锁的时间
			String oldValue = (String) opsForValue.getAndSet(key, value);
			if(CoreUtils.isNotEmpty(oldValue) && oldValue.equals(currentValue)){
				return true;
			}
		}
    	return false;
    }
    /**
     * string 
     * 锁
     * @param key
     * @param value
     * @return
     */
   public void unlockString(final String key, Object value){
	   String currentValue = (String) get(key);
		if(CoreUtils.isNotEmpty(currentValue) && currentValue.equals(value)){
			remove(key);
		}
    }
}